package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import bean.BusType;
import bean.Driver;
import tools.DBTools;
public class BusStopDaoImpl implements BusStopDao{
    //与数据库建立连接
    Connection conn = DBTools.getConn();
    PreparedStatement pstmt = null;
    @Override
    public String[] getColumnNames(String tableName) {
        // TODO Auto-generated method stub
        String[] columnNames = null;

        String sql = "select * from " + tableName;

        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();

            columnNames = new String[rsmd.getColumnCount()];
            for (int i = 0; i < columnNames.length; i++) {
                columnNames[i] = rsmd.getColumnLabel(i + 1);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return columnNames;
    }

    @Override
    public List<BusType> getAllbuses() {
        // TODO Auto-generated method stub
        List<BusType> buslist = null;
        String sql = "select * from businfortable";
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            buslist = new ArrayList<BusType>();
            BusType bus = null;
            while(rs.next()) {
                bus = new BusType();
                bus.setBid(rs.getInt(1));
                bus.setDid(rs.getInt(2));
                bus.setBnum(rs.getString(3));
                bus.setBtype(rs.getString(4));
                bus.setBusstop(rs.getString(5));
                buslist.add(bus);	//将这条记录添入列表
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return buslist;
    }

    @Override
    public int addBus(BusType bus) {
        // TODO Auto-generated method stub
        Connection conn=DBTools.getConn();
        int i=0;
        String sql="insert into businfortable(bid,did,bnum,btype,busstop) values(?,?,?,?,?)";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, bus.getBid());
            pstmt.setInt(2, bus.getDid());
            pstmt.setString(3, bus.getBnum());
            pstmt.setString(4, bus.getBtype());
            pstmt.setString(5, bus.getBusstop());
            i=pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBTools.close(conn, pstmt, null);
        }
        return i;
    }

    @Override
    public int updateBus(BusType bus) {
        // TODO Auto-generated method stub
        Connection conn=DBTools.getConn();
        int i=0;
        String sql="update businfortable set did=?,btype=?,busstop=? where bnum=?";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, bus.getDid());
            pstmt.setString(4, bus.getBnum());
            pstmt.setString(2, bus.getBtype());
            pstmt.setString(3, bus.getBusstop());
            i=pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBTools.close(conn, pstmt, null);
        }
        return i;
    }

    @Override
    public int deleteBus(String isbn) {
        // TODO Auto-generated method stub

        int i=0;
        String sql = "delete from businfortable where bid=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, isbn);
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return i;
    }



    @Override
    public List<BusType> getBusbySth(String bySth, String keyWord) {
        // TODO Auto-generated method stub
        List<BusType> buslist=null;
        String sql="";
        //定义要执行的sql语句
        sql = "select * from businfortable where "+bySth+" like concat('%',?,'%')";

        //定义要执行的prepareStamtent类对象
        try {
            pstmt = conn.prepareStatement(sql);
            //给？赋值
            pstmt.setString(1, keyWord);
            ResultSet rs = pstmt.executeQuery();
            buslist = new ArrayList<BusType>();
            BusType bus = null;
            while(rs.next()){
                bus=new BusType();
                bus.setBid(rs.getInt(1));
                bus.setDid(rs.getInt(2));
                bus.setBnum(rs.getString(3));
                bus.setBtype(rs.getString(4));
                bus.setBusstop(rs.getString(5));
                buslist.add(bus);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        return buslist;
    }



}